<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>boost/python/to_python_converter.hpp</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Boost.Python Reference Manual">
<link rel="up" href="../to_from_python_type_conversion.html" title="Chapter 5. To/From Python Type Conversion">
<link rel="prev" href="boost_python_opaque_pointer_conv.html" title="boost/python/opaque_pointer_converter.hpp">
<link rel="next" href="boost_python_register_ptr_to_pyt.html" title="boost/python/register_ptr_to_python.hpp">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_opaque_pointer_conv.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../to_from_python_type_conversion.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_register_ptr_to_pyt.html"><img src="../../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="to_from_python_type_conversion.boost_python_to_python_converter"></a><a class="link" href="boost_python_to_python_converter.html" title="boost/python/to_python_converter.hpp">boost/python/to_python_converter.hpp</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="boost_python_to_python_converter.html#to_from_python_type_conversion.boost_python_to_python_converter.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="boost_python_to_python_converter.html#to_from_python_type_conversion.boost_python_to_python_converter.class_template_to_python_convert">Class
        template <code class="computeroutput"><span class="identifier">to_python_converter</span></code></a></span></dt>
<dt><span class="section"><a href="boost_python_to_python_converter.html#to_from_python_type_conversion.boost_python_to_python_converter.example">Example</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="to_from_python_type_conversion.boost_python_to_python_converter.introduction"></a><a class="link" href="boost_python_to_python_converter.html#to_from_python_type_conversion.boost_python_to_python_converter.introduction" title="Introduction">Introduction</a>
</h3></div></div></div>
<p>
          <code class="computeroutput"><span class="identifier">to_python_converter</span></code> registers
          a conversion from objects of a given C++ type into a Python object.
        </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="to_from_python_type_conversion.boost_python_to_python_converter.class_template_to_python_convert"></a><a class="link" href="boost_python_to_python_converter.html#to_from_python_type_conversion.boost_python_to_python_converter.class_template_to_python_convert" title="Class template to_python_converter">Class
        template <code class="computeroutput"><span class="identifier">to_python_converter</span></code></a>
</h3></div></div></div>
<div class="toc"><dl class="toc"><dt><span class="section"><a href="boost_python_to_python_converter.html#to_from_python_type_conversion.boost_python_to_python_converter.class_template_to_python_convert.class_template_to_python_convert">Class
          template <code class="computeroutput"><span class="identifier">to_python_converter</span></code>
          constructor</a></span></dt></dl></div>
<p>
          <code class="computeroutput"><span class="identifier">to_python_converter</span></code> adds
          a wrapper around a static member function of its second template parameter,
          handling low-level details such as insertion into the converter registry.
        </p>
<p>
          In the table below, x denotes an object of type T
        </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                  <p>
                    Parameter
                  </p>
                </th>
<th>
                  <p>
                    Requirements
                  </p>
                </th>
<th>
                  <p>
                    Description
                  </p>
                </th>
</tr></thead>
<tbody>
<tr>
<td>
                  <p>
                    T
                  </p>
                </td>
<td>
                </td>
<td>
                  <p>
                    The C++ type of the source object in the conversion
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    Conversion
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">PyObject</span><span class="special">*</span>
                    <span class="identifier">p</span> <span class="special">=</span>
                    <span class="identifier">Conversion</span><span class="special">::</span><span class="identifier">convert</span><span class="special">(</span><span class="identifier">x</span><span class="special">)</span></code>,
                    <code class="computeroutput"><span class="keyword">if</span> <span class="identifier">p</span>
                    <span class="special">==</span> <span class="number">0</span></code>,
                    <code class="computeroutput"><span class="identifier">PyErr_Occurred</span><span class="special">()</span> <span class="special">!=</span>
                    <span class="number">0</span></code>.
                  </p>
                </td>
<td>
                  <p>
                    A class type whose static member function convert does the real
                    work of the conversion.
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    bool has_get_pytype=false
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">PyTypeObject</span> <span class="keyword">const</span> <span class="special">*</span>
                    <span class="identifier">p</span> <span class="special">=</span>
                    <span class="identifier">Conversion</span><span class="special">::</span><span class="identifier">get_pytype</span><span class="special">()</span></code>
                  </p>
                </td>
<td>
                  <p>
                    Optional member - if Conversion has <code class="computeroutput"><span class="identifier">get_pytype</span></code>
                    member supply <code class="computeroutput"><span class="keyword">true</span></code>
                    for this parameters. If present <code class="computeroutput"><span class="identifier">get_pytype</span></code>
                    is used to document the return type of functions using this conversion.
                    The <code class="computeroutput"><span class="identifier">get_pytype</span></code>
                    may be implemented using the classes and functions from pytype_function.hpp
                    NOTE : For backward compatibility this parameter may be passed
                    after checking if BOOST_PYTHON_SUPPORTS_PY_SIGNATURES is defined
                    (see <a class="link" href="../function_invocation_and_creation/function_documentation.html#function_invocation_and_creation.function_documentation.boost_python_pytype_function_hpp.example" title="Example">here</a>).
                  </p>
                </td>
</tr>
</tbody>
</table></div>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
<span class="special">{</span>
  <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Conversion</span><span class="special">,</span> <span class="keyword">bool</span> <span class="identifier">convertion_has_get_pytype_member</span><span class="special">=</span><span class="keyword">false</span><span class="special">&gt;</span>
  <span class="keyword">struct</span> <span class="identifier">to_python_converter</span>
  <span class="special">{</span>
      <span class="identifier">to_python_converter</span><span class="special">();</span>
  <span class="special">};</span>
<span class="special">}}</span>
</pre>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="to_from_python_type_conversion.boost_python_to_python_converter.class_template_to_python_convert.class_template_to_python_convert"></a><a class="link" href="boost_python_to_python_converter.html#to_from_python_type_conversion.boost_python_to_python_converter.class_template_to_python_convert.class_template_to_python_convert" title="Class template to_python_converter constructor">Class
          template <code class="computeroutput"><span class="identifier">to_python_converter</span></code>
          constructor</a>
</h4></div></div></div>
<pre class="programlisting"><span class="identifier">to_python_converter</span><span class="special">();</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Effects</span></dt>
<dd><p>
                  Registers a <code class="computeroutput"><span class="identifier">to_python</span></code>
                  converter which uses <code class="computeroutput"><span class="identifier">Conversion</span><span class="special">::</span><span class="identifier">convert</span><span class="special">()</span></code> to do its work.
                </p></dd>
</dl>
</div>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="to_from_python_type_conversion.boost_python_to_python_converter.example"></a><a class="link" href="boost_python_to_python_converter.html#to_from_python_type_conversion.boost_python_to_python_converter.example" title="Example">Example</a>
</h3></div></div></div>
<p>
          This example presumes that someone has implemented the standard noddy example
          module from the Python documentation, and placed the corresponding declarations
          in "noddy.h". Because noddy_NoddyObject is the ultimate trivial
          extension type, the example is a bit contrived: it wraps a function for
          which all information is contained in the type of its return value.
        </p>
<p>
          In C++:
        </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">reference</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">module</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="string">"noddy.h"</span>

<span class="keyword">struct</span> <span class="identifier">tag</span> <span class="special">{};</span>
<span class="identifier">tag</span> <span class="identifier">make_tag</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">tag</span><span class="special">();</span> <span class="special">}</span>

<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>

<span class="keyword">struct</span> <span class="identifier">tag_to_noddy</span>
<span class="special">{</span>
    <span class="keyword">static</span> <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">convert</span><span class="special">(</span><span class="identifier">tag</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">PyObject_New</span><span class="special">(</span><span class="identifier">noddy_NoddyObject</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">noddy_NoddyType</span><span class="special">);</span>
    <span class="special">}</span>
    <span class="keyword">static</span> <span class="identifier">PyTypeObject</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">get_pytype</span><span class="special">()</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="special">&amp;</span><span class="identifier">noddy_NoddyType</span><span class="special">;</span>
    <span class="special">}</span>
<span class="special">};</span>

<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">to_python_converter</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">def</span><span class="special">(</span><span class="string">"make_tag"</span><span class="special">,</span> <span class="identifier">make_tag</span><span class="special">);</span>
    <span class="identifier">to_python_converter</span><span class="special">&lt;</span><span class="identifier">tag</span><span class="special">,</span> <span class="identifier">tag_to_noddy</span><span class="special">,</span> <span class="keyword">true</span><span class="special">&gt;();</span> <span class="comment">//"true" because tag_to_noddy has member get_pytype</span>
<span class="special">}</span>
</pre>
<p>
          In Python:
        </p>
<pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="identifier">import</span> <span class="identifier">to_python_converter</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">def</span> <span class="identifier">always_none</span><span class="special">():</span>
<span class="special">...</span>     <span class="keyword">return</span> <span class="identifier">None</span>
<span class="special">...</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">def</span> <span class="identifier">choose_function</span><span class="special">(</span><span class="identifier">x</span><span class="special">):</span>
<span class="special">...</span>     <span class="keyword">if</span> <span class="special">(</span><span class="identifier">x</span> <span class="special">%</span> <span class="number">2</span> <span class="special">!=</span> <span class="number">0</span><span class="special">):</span>
<span class="special">...</span>         <span class="keyword">return</span> <span class="identifier">to_python_converter</span><span class="special">.</span><span class="identifier">make_tag</span>
<span class="special">...</span>     <span class="keyword">else</span><span class="special">:</span>
<span class="special">...</span>         <span class="keyword">return</span> <span class="identifier">always_none</span>
<span class="special">...</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">a</span> <span class="special">=</span> <span class="special">[</span> <span class="identifier">choose_function</span><span class="special">(</span><span class="identifier">x</span><span class="special">)</span> <span class="keyword">for</span> <span class="identifier">x</span> <span class="identifier">in</span> <span class="identifier">range</span><span class="special">(</span><span class="number">5</span><span class="special">)</span> <span class="special">]</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">b</span> <span class="special">=</span> <span class="special">[</span> <span class="identifier">f</span><span class="special">()</span> <span class="keyword">for</span> <span class="identifier">f</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="special">]</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">type</span><span class="special">(</span><span class="identifier">b</span><span class="special">[</span><span class="number">0</span><span class="special">])</span>
<span class="special">&lt;</span><span class="identifier">type</span> <span class="char">'NoneType'</span><span class="special">&gt;</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">type</span><span class="special">(</span><span class="identifier">b</span><span class="special">[</span><span class="number">1</span><span class="special">])</span>
<span class="special">&lt;</span><span class="identifier">type</span> <span class="char">'Noddy'</span><span class="special">&gt;</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">type</span><span class="special">(</span><span class="identifier">b</span><span class="special">[</span><span class="number">2</span><span class="special">])</span>
<span class="special">&lt;</span><span class="identifier">type</span> <span class="char">'NoneType'</span><span class="special">&gt;</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">type</span><span class="special">(</span><span class="identifier">b</span><span class="special">[</span><span class="number">3</span><span class="special">])</span>
<span class="special">&lt;</span><span class="identifier">type</span> <span class="char">'Noddy'</span><span class="special">&gt;</span>
</pre>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_opaque_pointer_conv.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../to_from_python_type_conversion.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_register_ptr_to_pyt.html"><img src="../../images/next.png" alt="Next"></a>
</div>
</body>
</html>
